package com.zhuhjay.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhuhjay.exception.ServerErrorException;

/**
 * Jackson工具类
 * @author ZhuHJay
 * @date 2023/2/13 18:45
 */
public class JacksonUtils {

    private static final ObjectMapper MAPPER;
    static {
        MAPPER = SpringContextHolder.getBean(ObjectMapper.class);
    }

    public static String writeValueAsString(Object value) {
        try {
            return MAPPER.writeValueAsString(value);
        } catch (Exception e) {
            throw new ServerErrorException("JSON序列化失败");
        }
    }

    public static <T> T readValue(String content, Class<T> valueType) {
        try {
            return MAPPER.readValue(content, valueType);
        } catch (Exception e) {
            throw new ServerErrorException("JSON反序列化失败");
        }
    }

}
